package com.visionet.school.app.web.service.tclass;

import com.visionet.school.app.web.common.utils.PageUtils;
import com.visionet.school.app.web.entity.enums.MemberType;
import com.visionet.school.app.web.entity.member.Member;
import com.visionet.school.app.web.entity.member.MemberClass;
import com.visionet.school.app.web.entity.member.vo.MemberClassVo;
import com.visionet.school.app.web.entity.tclass.SchoolClass;
import com.visionet.school.app.web.entity.tclass.vo.SchoolClassVo;
import com.visionet.school.app.web.entity.vo.MemberVo;
import com.visionet.school.app.web.repository.member.MemberClassRepository;
import com.visionet.school.app.web.repository.member.MemberRepository;
import com.visionet.school.app.web.repository.tclass.SchoolClassRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/1.
 */

@Service
public class SchoolClassService {

    private static final Logger logger = LoggerFactory.getLogger(SchoolClassService.class);

    @Resource
    private SchoolClassRepository schoolClassRepository;

    @Resource
    private MemberClassRepository memberClassRepository;

    @Resource
    private MemberRepository memberRepository;


    public SchoolClass findOneById(String id){
        return schoolClassRepository.findOne(id);
    }

    public SchoolClass findOneByName(String name){
        return schoolClassRepository.findOneByClassName(name);
    }

    public Map<String, Object> getEntrustPage(final SchoolClassVo schoolClassVo, String sortType){
        Pageable pageable = PageUtils.buildPageRequest(schoolClassVo.getPageInfo().getPageNumber(), schoolClassVo.getPageInfo().getPageSize(), sortType);
        Page<SchoolClass> objPage = schoolClassRepository.findAll(new Specification<SchoolClass>() {
            public Predicate toPredicate(Root<SchoolClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> lstPredicates = new ArrayList<>();
//                if (StringUtils.isNotBlank(serArgs.get("serSbmc"))) {
//                    lstPredicates.add(cb.like(root.get("sbmc").as(String.class), "%" + serArgs.get("serSbmc") + "%"));
//                }
                lstPredicates.add(cb.equal(root.get("isDeleted"), "0"));
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
                return cb.and(lstPredicates.toArray(arrayPredicates));
            }
        }, pageable);
        return PageUtils.getPageMap(objPage);
    }

    public SchoolClassVo getClassMember(String id,String memberType){
        return schoolClassRepository.getClassMember(id,memberType);
    }

    public Map<String, Object> getClassPage(SchoolClassVo schoolClassVo, String sortType){
        Page<SchoolClassVo> objPage = schoolClassRepository.findClassPage(schoolClassVo,sortType);
        return PageUtils.getPageMap(objPage);
    }

    public void updateClassDetails(SchoolClassVo schoolClassVo){
        if(StringUtils.isNotBlank(schoolClassVo.getTeacherName())){
            MemberClass memberClass = memberClassRepository.findTeacherByClassId(schoolClassVo.getId());
            if(memberClass == null){
                memberClass = new MemberClass();
                memberClass.setClassId(schoolClassVo.getId());
                memberClass.setIsDeleted("0");
                memberClass.setMemberId(schoolClassVo.getTeacherId());
                memberClass.setMemberType(MemberType.TEACHER.toKey());
            }else{
                memberClass.setMemberId(schoolClassVo.getTeacherId());
            }
            memberClassRepository.save(memberClass);
        }
        if(StringUtils.isNotBlank(schoolClassVo.getClassInfomation())||StringUtils.isNoneBlank(schoolClassVo.getClassName())){
            SchoolClass schoolClass = schoolClassRepository.findOne(schoolClassVo.getId());
            schoolClass.setClassName(schoolClassVo.getClassName());
            schoolClass.setClassInfomation(schoolClassVo.getClassInfomation());
            schoolClassRepository.save(schoolClass);
        }
    }


    public SchoolClassVo findClassByMemberId(String memberId){
        MemberClass memberClass = memberClassRepository.findOneByMemberId(memberId);
        if(memberClass == null){
            return null;
        }
        SchoolClass schoolClass = schoolClassRepository.findOneById(memberClass.getClassId());
        SchoolClassVo schoolClassVo = new SchoolClassVo();
        BeanUtils.copyProperties(schoolClass,schoolClassVo);
        Member member = new Member();
        if(memberClass.getMemberType().equals(MemberType.TEACHER.toKey())){
            member = memberRepository.findOne(memberId);
        }else{
            List<MemberClass> list = memberClassRepository.findListByClassId(memberClass.getClassId(), MemberType.TEACHER.toKey());
            if(list == null || list.size() < 1){
                return null;
            }
            member = memberRepository.findOne(list.get(0).getMemberId());
        }
        if(member== null || StringUtils.isBlank(member.getId())){
            return null;
        }
        schoolClassVo.setTeacherName(member.getMemberName());
        schoolClassVo.setTeacherId(member.getId());
        return schoolClassVo;
    }

    public Map<String, Object> getStudentList(MemberClassVo memberClassVo){
        Page<MemberVo> objPage = memberClassRepository.findStudentByClassId(memberClassVo);
        return PageUtils.getPageMap(objPage);
    }

}
